package com.oocl.manager;

import com.oocl.consumer.MsgConsumer;
import com.oocl.consumer.impl.MsgConsumerImpl;
import com.oocl.dao.BookDao;
import com.oocl.dao.impl.BookDaoImpl;

public class MsgConsumerManager {

	public static volatile int count = 0;
	public static final int MAX_COUNT = 10000;
	public static long startTime;
	public static long endTime;
	
	private BookDao bookDao;
	
	private static MsgConsumerManager sInstance = new  MsgConsumerManager();
	
	private MsgConsumerManager(){
		bookDao = new BookDaoImpl();
	}
	
	public static MsgConsumerManager getInstance(){
		return sInstance;
	}

	public void startReceiveMsg(){
		final MsgConsumer consumerA = new MsgConsumerImpl(this.bookDao);
		final MsgConsumer consumerB = new MsgConsumerImpl(this.bookDao);
		final MsgConsumer consumerC = new MsgConsumerImpl(this.bookDao);
		final MsgConsumer consumerD = new MsgConsumerImpl(this.bookDao);
		
		startTime = System.currentTimeMillis();
		
		Thread T1 = new Thread(){
			public void run() {
				while(true){
					if(MsgConsumerManager.count>=MsgConsumerManager.MAX_COUNT){
						break;
					}
				}
				consumerA.offLine();
				consumerB.offLine();
				consumerC.offLine();
				consumerD.offLine();
				endTime = System.currentTimeMillis();
				System.out.println("总共耗时： "+ (endTime-startTime)+ " ms.");
			}
		};
		T1.start();
		consumerA.onLine();
		consumerB.onLine();
		consumerC.onLine();
		consumerD.onLine();
	}
	
}
